# Endline covariates ------------------------------------------------------


# Create numeric version of all variables
el_pilot <- 
	el_pilot %>% 
	mutate(
		mud_floor = case_when(
			is.na(floor_q16_6) ~ NA_real_,
			floor_q16_6 == 2 ~ 1, 
			floor_q16_6 == 1 ~ 1, 
			TRUE ~ 0
		),
		electric_light = case_when(
			is.na(light_q16_7) ~ NA_real_,
			light_q16_7 == 7 ~ 1, 
			light_q16_7 == 5 ~ 1, 
			TRUE ~ 0
		),
		no_educ = as.numeric(educ_q2_21 == 18),
		female = as.numeric(sex_q2_1 == 2),
		lantern = as.numeric(lantern_q17_17 == 1),
		brick_walls = as.numeric(walls_q16_5 == 5),
		motorcycle = as.numeric(motor_cycle_q17_15 == 1),
		sofa = as.numeric(sofa_q17_8 == 1),
		tv = as.numeric(tv_q17_2 == 1),
		radio = as.numeric(radio_q17_1 == 1),
		chair = as.numeric(chair_q17_7 == 1),
		moth_fath = as.numeric(moth_fath_16 == 1),
		big_city = as.numeric(big_city_q2_14 == 1),
		rooms = rooms_q16_2,
		age = age_q2_3
	)



el_pilot <- el_pilot %>%
	mutate(walk3 = ifelse(walk3_q2_20 <10, walk3_q2_20-1, NA),
				 mobile_use = ifelse(mobile_q4_2<10,mobile_q4_2-1,NA),
				 ppl_known = ifelse(people_known_q2_13<10,
				 									 4-people_known_q2_13, NA),
				 watch_tv = case_when(
				 	is.na(watchTV_q4_1) ~ NA_real_,
				 	watchTV_q4_1 < 6 ~ as.numeric(watchTV_q4_1),
				 	watchTV_q4_1==6 ~ 0
				 ),
				 literacy = case_when(
				 	is.na(read_q2_22) ~ NA_real_,
				 	read_q2_22 == 1 ~ 1,
				 	read_q2_22 == 3 ~ 1,
				 	read_q2_22 == 2 ~ 2,
				 	read_q2_22 == 4 ~ 0
				 ),
				 asset_index = tv + radio + chair + sofa + rooms + 
				 	(1-mud_floor) + motorcycle,
					 
				 education = case_when(
				 	is.na(educ_q2_21) ~ NA_character_,
				 	educ_q2_21 == 18 ~ "None",
				 	educ_q2_21 < 8 | educ_q2_21 == 15 ~"Primary Education",
				 	educ_q2_21 > 7 & educ_q2_21 < 14 ~ "Secondary Education",
				 	educ_q2_21 == 14 ~ "Tertiary Education",
				 	educ_q2_21 == 19 ~ "Tertiary Education"
				 ),
				 married = case_when(
				 	is.na(status_q2_5) ~ NA_character_,
				 	status_q2_5 == 1 ~ "Married or Living As Married",
				 	status_q2_5 == 2 ~ "Married or Living As Married",
				 	TRUE ~ "Not Married"),
				 english_christian = case_when(
				 	is.na(religion_q14_1) ~ NA_real_,
				 	religion_q14_1 == 5 ~ 1,
				 	religion_q14_1 == 6 ~ 1,
				 	TRUE ~ 0),
				 catholic = ifelse(religion_q14_1 == 3, 1, 0),
				 holy_spirit = case_when(
				 	is.na(religion_q14_1) ~ NA_real_,
				 	religion_q14_1 == 7 ~ 1,
				 	religion_q14_1 == 8 ~ 1,
				 	religion_q14_1 == 9 ~ 1,
				 	religion_q14_1 == 10 ~ 1,
				 	TRUE ~ 0),
				 christian_only = ifelse(religion_q14_1 == 2, 1, 0),
				 protestant = ifelse(religion_q14_1 == 4, 1, 0),
				 muslim = ifelse(religion_q14_1 == 11, 1, 0),
				 atheist = case_when(
				 	is.na(religion_q14_1) ~ NA_real_,
				 	religion_q14_1 == 1 ~ 1,
				 	religion_q14_1 == 15 ~ 1,
				 	religion_q14_1 == 16 ~ 1,
				 	TRUE ~ 0),
				 minority_religion = case_when(
				 	is.na(religion_q14_1) ~ NA_real_,
				 	religion_q14_1 == 13 ~ 1,
				 	religion_q14_1 == 14 ~ 1,
				 	religion_q14_1 == 17 ~ 1,
				 	TRUE ~ 0),
				 read_write_int = ifelse(!is.na(read_q2_22b), read_q2_22b,read_q2_22 ),
				 read_only = ifelse(read_write_int == 1, 1, 0),
				 write_and_read = ifelse(read_write_int == 2, 1, 0),
				 write_only = ifelse(read_write_int == 3, 1, 0),
				 illiterate  = ifelse(read_write_int == 4, 1, 0),
				 mud_wall = ifelse(walls_q16_5<4, 1,0),
				 stone_wall = case_when(
				 	is.na(walls_q16_5) ~ NA_real_, 
				 	walls_q16_5 == 4 ~ 1,
				 	walls_q16_5 == 6 ~ 1,
				 	TRUE ~ 0),
				 cement_wall= ifelse(walls_q16_5 == 7, 1, 0),
				 misc_wall = case_when(
				 	is.na(walls_q16_5) ~ NA_real_,
				 	walls_q16_5 == 8 ~ 1,
				 	walls_q16_5 == 9 ~ 1,
				 	walls_q16_5 == 10 ~ 1,
				 	walls_q16_5 == 11 ~ 1,
				 	TRUE ~ 0),
				 brick_wall = brick_walls,
				 muganda_tribe = as.numeric(tribe_q2_25 == 1),
				 mufumbira_tribe = as.numeric(tribe_q2_25 == 14),
				 mukiga = as.numeric(tribe_q2_25 == 18),
				 munyankole = as.numeric(tribe_q2_25 == 21),
				 munyarwanda = as.numeric(tribe_q2_25 == 22),
				 munyoro = as.numeric(tribe_q2_25 == 24),
				 mutooro =  as.numeric(tribe_q2_25 == 29),
				 minority_tribe = case_when(
				 	is.na(tribe_q2_25) ~ NA_real_,
				 	tribe_q2_25 == 1 ~ 0,
				 	tribe_q2_25 == 14 ~ 0,
				 	tribe_q2_25 == 18 ~ 0,
				 	tribe_q2_25 == 21 ~ 0,
				 	tribe_q2_25 == 22 ~ 0,
				 	tribe_q2_25 == 24 ~ 0,
				 	tribe_q2_25 == 29 ~ 0,
				 	TRUE ~ 1),
				 cellphone = case_when(
				 	cellphone_q17_5 == 1 ~ 1,
				 	is.na(cellphone_q17_5) ~ NA_real_,
				 	TRUE ~ 0
				 ),
				 luganda_lang = as.numeric(lang_q2_23 == 2),
				 fumbira_lang = case_when(
				 	is.na(lang_q2_23) ~ NA_real_, 
				 	lang_q2_23 == 26 ~ 1, 
				 	lang_q2_23 == 30 ~ 1, 
				 	TRUE ~ 0),
				 runyannkole_lang = as.numeric(lang_q2_23 ==29),
				 minority_lang = case_when(
				 	is.na(lang_q2_23) ~ NA_real_,
				 	lang_q2_23 == 2 ~ 0,
				 	lang_q2_23 == 26 ~ 0,
				 	lang_q2_23 == 29 ~ 0,
				 	lang_q2_23 == 30 ~ 0,
				 	TRUE ~ 1),
				 pray_private = case_when(
				 	pray_private_q14_4 == 9 ~ 0,
				 	pray_private_q14_4 == 8 ~ 1,
				 	pray_private_q14_4 == 7 ~ 2,
				 	pray_private_q14_4 == 6 ~ 3,
				 	pray_private_q14_4 == 5 ~ 4,
				 	pray_private_q14_4 == 4 ~ 6,
				 	pray_private_q14_4 == 3 ~ 7,
				 	pray_private_q14_4 == 2 ~ 8,
				 	pray_private_q14_4 == 1 ~ 9,
				 	is.na(pray_private_q14_4) ~ NA_real_
				 ),
				 religious_service = religion_times_q14_2,
				 electric_light =as.numeric(light_q16_7 == 7),
				 kerosene_light = as.numeric(light_q16_7 == 1),
				 solar_light = as.numeric(light_q16_7 == 5),
				 misc_light = case_when(
				 	is.na(light_q16_7) ~ NA_real_,
				 	light_q16_7 == 1 ~ 0,
				 	light_q16_7 == 5 ~ 0,
				 	light_q16_7 == 7 ~ 0,
				 	TRUE ~ 1),
				 same_village = as.numeric(vill16_q2_15 == 1),
				 other_person =  as.numeric(person_q18_1 == 1),
				 charcoal_fuel = as.numeric(fuel_q16_8 == 2),
				 firewood_fuel = as.numeric(fuel_q16_8 == 3),
				 misc_fuel = case_when(
				 	is.na(fuel_q16_8) ~ NA_real_,
				 	fuel_q16_8 == 2 ~ 0, 
				 	fuel_q16_8 == 3 ~ 0,
				 	TRUE ~ 1),
				 household_children = ifelse(
				 	members_q2_6==1, 0, 
				 	child_q2_7),
				 household_older = ifelse(members_q2_6==1,
				 												 0, older_q2_8),
				 household_younger = ifelse(
				 	members_q2_6==1,
				 	0, younger_q2_9),
				 members = members_q2_6,
				 highest_grade = case_when(
				 	is.na(educ_q2_21) ~ NA_real_,
				 	educ_q2_21 < 14 ~ as.numeric(educ_q2_21),
				 	educ_q2_21 == 14 ~ 15,
				 	educ_q2_21 == 15 ~ 14,
				 	educ_q2_21 == 15 ~ 14,
				 	educ_q2_21 == 19 ~ 16,
				 	educ_q2_21 == 18 ~ 0
				 ),
				 university = as.numeric(highest_grade == 16),
				 household_head = as.numeric(rel_q2_4 ==1),
				 household_spouse = as.numeric(rel_q2_4 ==2),
				 
				 household_other = case_when(
				 	is.na(rel_q2_4) ~ NA_real_,
				 	rel_q2_4 == 1 ~ 0,
				 	rel_q2_4 == 2 ~ 0,
				 	TRUE ~ 1),
				 
				 earth_floor = as.numeric(floor_q16_6 == 1),
				 
				 cement_floor = as.numeric(floor_q16_6 == 7),
				 
				 misc_floor = case_when(
				 	is.na(floor_q16_6) ~ NA_real_,
				 	floor_q16_6 == 1 ~ 0,
				 	floor_q16_6 == 7 ~ 0,
				 	TRUE ~ 1),
				 
				 
				 education_work = case_when(
				 	is.na(activity_q3_1) ~ NA_real_,
				 	activity_q3_1 == 16 ~ 1,
				 	activity_q3_1 == 17 ~ 1,
				 	TRUE ~ 0),
				 domestic_work =  as.numeric(activity_q3_1 == 12),
				 other_work = as.numeric(activity_q3_1 == 15),
				 agriculture_work = case_when(
				 	is.na(activity_q3_1) ~ NA_real_,
				 	activity_q3_1 == 1 ~ 1,
				 	activity_q3_1 == 3 ~ 1,
				 	TRUE ~ 0),
				 retail_work = case_when(
				 	is.na(activity_q3_1) ~ NA_real_,
				 	activity_q3_1 == 7 ~ 1,
				 	TRUE ~ 0),
				 transport_work = case_when(
				 	is.na(activity_q3_1) ~ NA_real_,
				 	activity_q3_1 == 13 ~ 1,
				 	TRUE ~ 0),
				 manual_work = case_when(
				 	is.na(activity_q3_1) ~ NA_real_,
				 	activity_q3_1 == 4 ~ 1,
				 	activity_q3_1 == 5 ~ 1,
				 	activity_q3_1 == 6 ~ 1,
				 	activity_q3_1 == 10 ~ 1,
				 	activity_q3_1 == 11 ~ 1,
				 	TRUE ~ 0),
				 
				 hospitality_work = case_when(
				 	is.na(activity_q3_1) ~ NA_real_,
				 	activity_q3_1 == 8 ~ 1,
				 	activity_q3_1 == 9 ~ 1,
				 	TRUE ~ 0),
				 
				 no_work = case_when(
				 	is.na(activity_q3_1) ~ NA_real_,
				 	activity_q3_1 == 14 ~ 1,
				 	TRUE ~ 0),
				 
				 several_huts = ifelse(dwell_q16_1 == 1, 1, 0),
				 single_hut = ifelse(dwell_q16_1 == 2, 1, 0),
				 share_house = ifelse(dwell_q16_1 == 3, 1, 0),
				 
				 day = case_when(
				 	is.na(day_q2_2) ~ NA_real_,
				 	day_q2_2 == 3 ~ 0,
				 	day_q2_2 == 1 ~ 1,
				 	day_q2_2 == 2 ~ 2
				 ),
				 
				 married = ifelse(status_q2_5 == 1, 1, 0),
				 living_as_married  = ifelse(status_q2_5 == 2, 1, 0),
				 not_married  = case_when(
				 	is.na(status_q2_5) ~ NA_real_,
				 	status_q2_5 == 7 ~ 1,
				 	status_q2_5 == 3 ~ 1,
				 	TRUE ~ 0), 
				 separated  = case_when(
				 	is.na(status_q2_5) ~ NA_real_,
				 	status_q2_5 == 4 ~ 1,
				 	status_q2_5 == 5 ~ 1,
				 	status_q2_5 == 6 ~ 1,
				 	TRUE ~ 0),
				 number_children = born_q2_10,
				 
				 living_conditions = case_when(
				 	is.na(living_cond_q5_1) ~ NA_real_,
				 	living_cond_q5_1 == 1 ~ -1, 
				 	living_cond_q5_1 == 3 ~ 0, 
				 	living_cond_q5_1 == 2 ~ 1
				 ),
				 
				 rooms = rooms_q16_2,
				 
				 living_conditions_compared = case_when(
				 	is.na(observe_conditions_q18_4) ~ NA_real_,
				 	observe_conditions_q18_4 == 1 ~ 0, 
				 	observe_conditions_q18_4 == 2 ~ 1, 
				 	observe_conditions_q18_4 == 3 ~ 2, 
				 	observe_conditions_q18_4 == 4 ~ 3, 
				 	observe_conditions_q18_4 == 5 ~ 4
				 )
				 
				 
				 
	)




# Covariates used in analyses run at individual level
el_pilot_covariates <- c(
	"female","lantern","motorcycle","chair",
	"rooms","electric_light","mobile_use",
	"ppl_known","watch_tv","literacy",
	"moth_fath","brick_walls","asset_index","radio",
	"big_city","mud_floor",
	"age","sofa","tv",
	"christian_only",             
	"catholic",                 
	"protestant",                 
	"english_christian",         
	"holy_spirit",               
	"muslim",                   
	"atheist",                    
	"minority_religion",         
	"religious_service",         
	"pray_private",             
	"several_huts",               
	"single_hut",                
	"share_house",               
	"mud_wall",                 
	"stone_wall",                 
	"brick_wall",                
	"cement_wall",               
	"misc_wall",                
	"misc_floor",                 
	"cement_floor",              
	"earth_floor",               
	"kerosene_light",           
	"misc_light",                 
	"solar_light",               
	"misc_fuel",                 
	"charcoal_fuel",            
	"firewood_fuel",              
	"cellphone",                 
	"other_person",             
	"living_conditions_compared", 
	"day",                       
	"household_head",            
	"household_spouse",         
	"household_other",            
	"married",                   
	"living_as_married",         
	"not_married",              
	"separated",                  
	"members",                   
	"household_children",        
	"household_older",          
	"household_younger",          
	"number_children",          
	"same_village",             
	"highest_grade",              
	"university",               
	"read_only",                
	"write_and_read",           
	"write_only",                 
	"illiterate",               
	"fumbira_lang",             
	"luganda_lang",             
	"runyannkole_lang",           
	"minority_lang",            
	"muganda_tribe",            
	"mufumbira_tribe",          
	"mukiga",                     
	"munyankole",               
	"munyarwanda",              
	"munyoro",                  
	"mutooro",                    
	"minority_tribe",           
	"agriculture_work",         
	"education_work",           
	"domestic_work",              
	"retail_work",              
	"transport_work",           
	"manual_work",              
	"hospitality_work",           
	"no_work",                  
	"other_work",         
	"living_conditions") 



# Grab covariates to impute
covariate_imp <- el_pilot[,c(el_pilot_covariates)]

# Perform imputation
covariate_imp <- complete(mice(covariate_imp,m = 1, seed = 123456))

# Remove originals from endline data
el_pilot <- el_pilot[,-which(names(el_pilot) %in% c(el_pilot_covariates))]

# Put in the new imputed covariates
el_pilot <- data.frame(el_pilot,covariate_imp)


el_pilot$motor_cycle <- el_pilot$motorcycle
el_pilot$travel_big_city <- el_pilot$big_city
el_pilot$mobile_phone_use <- el_pilot$mobile_use

# Outcome variables -------------------------------------------------------


el_pilot <- 
	el_pilot %>% 
	mutate(
		fire_teach = ifelse(fire_teach_q10_1 == 1, 1, 0),
		pta = ifelse(pta_q10_2 == 1,1,0),
		bring_up = ifelse(bring_up_q10_3 == 1, 1, 0),
		assemble = ifelse(assemble_q10_4 == 1, 1, 0),
		# Switched direction here
		take_serious = 1 - ifelse(take_serious_q10_5 == 1,1,0),
		repeat_absence = ifelse(repeat_absense_q10_7 == 1,1,0),
		village_action = ifelse(village_action_q10_8 == 2, 1, 0),
		change_abs = ifelse(change_abs_q10_9 == 2,1,0), 
		school_abs = case_when(
			school_abs_q10_10 == 3 ~ 0,
			school_abs_q10_10 == 2 ~ 1,
			school_abs_q10_10 == 1 ~ 2,
			is.na(school_abs_q10_10) ~ NA_real_
		), 
		conversation_ABS = convers_q3_3_7,
		conversation_elections = convers_q3_3_6,
		ABS_discussion = convers_q3_3_7,
		elections_discussion = convers_q3_3_6,
		conversation_na = as.numeric((convers_q3_3_1 + 
																		convers_q3_3_2 + 
																		convers_q3_3_3 +
																		convers_q3_3_4 + 
																		convers_q3_3_5 + 
																		convers_q3_3_6 +  
																		convers_q3_3_7) == 0)
		
	)

responsibility <- el_pilot[,c("responsibility_q10_6_1",
												 "responsibility_q10_6_2",
												 "responsibility_q10_6_3",
												 "responsibility_q10_6_4",
												 "responsibility_q10_6_5")]

# The higher ranked parents are, the higher the resp scores out of 5
resp <- apply(
	X = responsibility,
	MARGIN = 1,
	FUN = function(resp_row) {
		5 - which(resp_row == 3)
	}
)

el_pilot$responsibility <- sapply(X = resp,FUN = function(x) x[1])

# Additional Outcome Variables --------------------------------------------

el_pilot$goal1_q6_3a
pilot_cb$goal1_q6_3a
pilot_cb$goal2_q6_3b
pilot_cb$goal3_q6_3c

question_6_3 <- 
	el_pilot %>% 
	select(goal1_q6_3a,
				 goal2_q6_3b,
				 goal3_q6_3c)

el_pilot$goal_ABS <-  apply(question_6_3,1, function(x) ifelse(4 %in% x, 1, 0))
el_pilot$goal_na <- apply(question_6_3,1, function(x) ifelse(sum(is.na(x)) ==3, 1, 0))


# Create compliance variables ---------------------------------------------

# For el_pilot
pilot_cb$films_q17_1
pilot_cb$other_attend_q17_2

#             Answer to 19.1)                       Answer to 19.2)
# complrs     1,2,3,4,5, or 6                       anything
# ind compl   0 but knew about screenings           Yes, friends and family
#             0 did not know about the screenings   Yes, friends
#             Don’t know                            Yes, family
#             Refuse to answer
# appr. nt    0 but knew about screenings           No
#                                                   Don't know 
#                                                   refuse
# never-takr  0 did not know about screenings       No
#             Don't know                            Don't know
#             Refuse to answer                      Refuse


el_pilot$compliance <- NA
el_pilot$compliance[el_pilot$films_q17_1 %in% 1:4] <- 1
el_pilot$compliance[
	# Not a complier 
	!(el_pilot$films_q17_1 %in% 1:4 | is.na(el_pilot$films_q17_1)) & 
		# But did have friends or family
		el_pilot$other_attend_q17_2 %in% c(1:3)
	] <- 2
el_pilot$compliance[el_pilot$films_q17_1 == 5 & (el_pilot$other_attend_q17_2 == 4 | is.na(el_pilot$other_attend_q17_2))] <- 3

el_pilot$compliance[
	(el_pilot$films_q17_1 == 6 | is.na(el_pilot$films_q17_1)) & 
		(el_pilot$other_attend_q17_2 == 4 | is.na(el_pilot$other_attend_q17_2))] <- 4

el_pilot$compliance[is.na(el_pilot$compliance)] <- 4


el_pilot$compliance_label <- NA
el_pilot$compliance_label[el_pilot$compliance == 1] <- "Complier"
el_pilot$compliance_label[el_pilot$compliance == 2] <- "Indirect Complier"
el_pilot$compliance_label[el_pilot$compliance == 3] <- "Apprised Never-Taker"
el_pilot$compliance_label[el_pilot$compliance == 4] <- "Never-Taker"


el_pilot$compliance_simple <- el_pilot$compliance
el_pilot$compliance_simple[el_pilot$compliance_label %in% c("Never-Taker", "Apprised Never-Taker")] <- 3
table(el_pilot$compliance_simple,useNA = "always")


# Create lists that will be used in analysis formulae and elsewhere

# Attitude outcomes for ABS
ABS_outcomes <- c(
	"fire_teach",
	"pta",
	"bring_up",
	"assemble")



# Imputation of missing outcome items -------------------------------------

el_pilot <- arrange(el_pilot, KEY)


# Impute missing absenteeism outcomes

ABS_imp <- el_pilot[,ABS_outcomes]

if(any(is.na(ABS_imp))) {
	ABS_imp <- complete(mice(ABS_imp,m = 1,seed = 1234567))
}


# Overwrite original outcome data
el_pilot <- el_pilot[,-which(names(el_pilot) %in% c(ABS_outcomes))]

# Reinsert the imputed data back into main data.frame
el_pilot <- data.frame(
	el_pilot,
	ABS_imp
)

# Build index

el_pilot$absenteeism_action = 
	(el_pilot$fire_teach + el_pilot$pta + el_pilot$bring_up + el_pilot$assemble) / 4









